<html>
<head><meta charset="utf-8"><title>Object safe generic methods? · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Object.20safe.20generic.20methods.3F.html">Object safe generic methods?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="226129728"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Object%20safe%20generic%20methods%3F/near/226129728" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Ze&#x27;ev Maor <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Object.20safe.20generic.20methods.3F.html#226129728">(Feb 12 2021 at 12:20)</a>:</h4>
<p>Currently object safety precludes generic methods, the check in the code in <a href="http://object_safety.rs">object_safety.rs</a> is commented with:<br>
    // We can't monomorphize things like <code>fn foo&lt;A&gt;(...)</code>.</p>
<p>Why is that?</p>
<p>While it's clear why referring to erased concrete types like Self precludes a method from being called through a vtable implementation, it's not quite clear why the compiler can't monomorphize generic trait methods based on all the calls found in the compiled code like monomorphizing simple generic functions, and populating all the vtables of the types implementing the trait with all the monomorphized implementations.</p>
<p>What am I missing here?</p>



<a name="226171213"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Object%20safe%20generic%20methods%3F/near/226171213" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Object.20safe.20generic.20methods.3F.html#226171213">(Feb 12 2021 at 17:45)</a>:</h4>
<p>"all the calls" mixed poorly with separate compilation, and doesn't work at all for anything that can do runtime code loading.</p>



<a name="226171674"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Object%20safe%20generic%20methods%3F/near/226171674" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Object.20safe.20generic.20methods.3F.html#226171674">(Feb 12 2021 at 17:47)</a>:</h4>
<p>It could perhaps be done for all the calls in a crate, but at that point one can also just manually put a method for each type on the trait.</p>



<a name="226193918"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Object%20safe%20generic%20methods%3F/near/226193918" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Ze&#x27;ev Maor <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Object.20safe.20generic.20methods.3F.html#226193918">(Feb 12 2021 at 20:47)</a>:</h4>
<p><span class="user-mention" data-user-id="125270">@scottmcm</span> Both of these arguments would apply to simple (non-trait) generic functions as well wouldn't they?</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>